package com.ychong.library.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.TypedValue
import android.view.*
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ychong.library.R
import com.ychong.library.adapter.BaseBindingAdapter
import com.ychong.library.data.SelectBean
import com.ychong.library.databinding.DialogBottomListBinding
import com.ychong.library.databinding.ItemTextBinding
import com.ychong.library.listener.OnListener
import com.ychong.library.utils.ClickUtils
import com.ychong.library.utils.ResUtils

class BottomListDialog(private val mContext: Context) :
    Dialog(mContext, R.style.dialog_bottom_full), View.OnClickListener {
    private var binding: DialogBottomListBinding? = null
    private var mDimAmount: Float = 0.2f
    private var dataList: MutableList<SelectBean> = ArrayList()
    private var selectListener: SelectListener? = null
    private var mGravity:Int = Gravity.CENTER
    private var mTextColor:Int? = null
    private var mTextSize:Float = 18f
    fun setSelectListener(selectListener: SelectListener): BottomListDialog {
        this.selectListener = selectListener
        return this
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding =
            DataBindingUtil.inflate(layoutInflater,
                R.layout.dialog_bottom_list, null, false)
        setContentView(binding!!.root)
        val window = window
        window?.setGravity(Gravity.BOTTOM)
        window?.setDimAmount(mDimAmount)
        window?.setWindowAnimations(R.style.share_animation)
        window?.setLayout(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT
        )
        binding!!.cancelTv.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        if (ClickUtils.isFastDoubleClick(1000)) {
            return
        }
        when (v.id) {
            R.id.cancel_tv -> {
                dismiss()
            }
        }
    }

    /**
     * dialog弹出的时候不改变顶部状态栏
     */
    fun clearFlags():BottomListDialog{
        window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        return this
    }

    fun setTextGravity(gravity:Int): BottomListDialog {
        this.mGravity = gravity
        return this
    }
    fun setDataList(list: MutableList<SelectBean>): BottomListDialog {
        dataList.addAll(list)
        return this
    }

    fun setTextColor(textColor:Int): BottomListDialog {
        this.mTextColor = textColor
        return this
    }
    fun setTextSize(textSize:Float): BottomListDialog {
        this.mTextSize = textSize
        return this
    }

    fun build() {
        super.show()
        setViewData()
    }

    private fun setViewData() {
        val myAdapter = MyAdapter(context,mGravity,mTextColor,mTextSize).apply {
            setItems(dataList)
        }
        binding!!.recycler.run {
            layoutManager = LinearLayoutManager(context)
            adapter = myAdapter
        }

        myAdapter.setItemClickListener(object : MyAdapter.ItemClickListener {
            override fun click(position: Int) {
                selectListener?.select(dataList[position])
                dismiss()
            }
        })

    }

    override fun dismiss() {
        super.dismiss()
        selectListener?.dismiss()
    }

    interface SelectListener {
        fun select(selectBean: SelectBean)
        fun dismiss()
    }

    class MyAdapter(var context: Context,var gravity:Int = Gravity.CENTER,var textColor:Int?,var textSize: Float) : BaseBindingAdapter<ItemTextBinding, SelectBean>() {
        private var listener: ItemClickListener? = null
        fun setItemClickListener(listener: ItemClickListener) {
            this.listener = listener
        }

        override fun onBindItem(binding: ItemTextBinding?, item: SelectBean, position: Int) {
            binding!!.textTv.gravity = gravity
            binding.textTv.setTextSize(TypedValue.COMPLEX_UNIT_SP,textSize)
            if (textColor!=null){
                binding.textTv.setTextColor(
                    ResUtils.getColor(
                        context,
                        textColor!!
                    )
                )
            }
            binding.textTv.text = item.text
            if (item.isSelect) {
                binding.textTv.setTextColor(
                    ResUtils.getColor(
                        context,
                        R.color.color_0D71DF
                    )
                )
            } else {
                binding.textTv.setTextColor(
                    ResUtils.getColor(
                        context,
                        R.color.color_666666
                    )
                )
            }
            binding.itemLayout.setOnClickListener {
                listener?.click(position)
            }
        }

        override fun getLayoutId(viewType: Int): Int {
            return R.layout.item_text
        }

        interface ItemClickListener {
            fun click(position: Int)
        }

    }

}